#ifndef BSP_CAN_RECIEVE_H
#define BSP_CAN_RECIEVE_H

#include "struct_typedef.h"
#include "can.h"
#include "bsp.h"




#define MOTOR_ECD_TO_ANGLE  (360.0 / 8191.0 / (3591.0/187.0))
#define MOTOR_ECD_TO_RAD 0.000766990394f
#define rad_format(Ang) loop_fp32_constrain((Ang), (-4096*MOTOR_ECD_TO_RAD), (4095*MOTOR_ECD_TO_RAD))
#define rad(code) ((code)*MOTOR_ECD_TO_RAD)




typedef struct
{
    uint16_t ecd;           
    int16_t speed_rpm;      
    int16_t given_current;  
    uint16_t temperate;     
    int16_t last_ecd;      
		int16_t round_cnt;      
		int16_t total_angle;    
		uint16_t offset_ecd;    
		uint32_t msg_cnt;      
} motor_measure_t;


//CAN识别
typedef enum { 
  BSP_CAN_1,
  BSP_CAN_2,
	BSP_CAN_NUM,
  BSP_CAN_ERR,
}BSP_CAN_t;

//CAN错误识别
typedef enum {
  HAL_CAN_TX_MAILBOX0_CPLT_CB,
  HAL_CAN_TX_MAILBOX1_CPLT_CB,
  HAL_CAN_TX_MAILBOX2_CPLT_CB,
  HAL_CAN_TX_MAILBOX0_ABORT_CB,
  HAL_CAN_TX_MAILBOX1_ABORT_CB,
  HAL_CAN_TX_MAILBOX2_ABORT_CB,
  HAL_CAN_RX_FIFO0_MSG_PENDING_CB,
  HAL_CAN_RX_FIFO0_FULL_CB,
  HAL_CAN_RX_FIFO1_MSG_PENDING_CB,
  HAL_CAN_RX_FIFO1_FULL_CB,
  HAL_CAN_SLEEP_CB,
  HAL_CAN_WAKEUP_FROM_RX_MSG_CB,
  HAL_CAN_ERROR_CB,
  BSP_CAN_CB_NUM
} BSP_CAN_Callback_t;


CAN_HandleTypeDef *BSP_CAN_GetHandle(BSP_CAN_t can);
int8_t BSP_CAN_RegisterCallback(BSP_CAN_t can, BSP_CAN_Callback_t type,void (*callback)(void));



void CAN_Motor_SendData(CAN_HandleTypeDef * hcan,uint16_t Send_Message_ID,uint16_t Motor_ID,int16_t Motor_data);//单电机控制



void CAN_cmd_chassis(int16_t motor1, int16_t motor2, int16_t motor3, int16_t motor4);//四电机控制，适用于0x201-204



static void (*CAN_Callback[BSP_CAN_NUM][BSP_CAN_CB_NUM])(void);

static BSP_CAN_t CAN_Get(CAN_HandleTypeDef *hcan) ;

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan);

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) ;

CAN_HandleTypeDef *BSP_CAN_GetHandle(BSP_CAN_t can) ;

int8_t BSP_CAN_RegisterCallback(BSP_CAN_t can, BSP_CAN_Callback_t type,void (*callback)(void)) ;

extern const motor_measure_t *get_motor_measure_point(uint8_t i);

#endif



